package cn.dongqihong.hehe;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashSet;
import java.util.regex.Pattern;

/**
 * Created by DongQihong on 2017/12/02.
 */
@WebServlet(name = "indexServlet", urlPatterns = "/img")
public class Index extends HttpServlet {

    private static final long serialVersionUID = -4377820862232402359L;
    private HashSet<String> setPhone = new HashSet<String>();

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ImgFontByte imgFontByte = ImgFontByte.getInstance();

        request.setCharacterEncoding("UTF-8");
        String pressText = request.getParameter("name");
        String phone = request.getParameter("phone");
        if (pressText == null || "".equals(pressText)) {
            pressText = "老板";
        }
        String srcImageFile = request.getSession().getServletContext().getRealPath("/") + "WEB-INF/image/1.jpg";
        srcImageFile = srcImageFile.replace("\\", "/");
        File img = new File(srcImageFile);
        Image src = ImageIO.read(img);
        int width = src.getWidth(null);
        int height = src.getHeight(null);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        g.drawImage(src, 0, 0, width, height, null);

//        g.setFont(new Font(Font.SERIF, Font.BOLD, 50));
        g.setFont(imgFontByte.getFont(48));
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1));
        g.setColor(new Color(20, 20, 20));
        g.drawString(pressText, 240, 726);
        g.drawString(pressText, 240, 725);
        g.setColor(Color.WHITE);
        g.drawString(pressText, 240, 720);
        g.setStroke(new BasicStroke(3));

        g.dispose();

        response.setContentType("image/jpeg");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        ImageIO.write(image, "JPEG", response.getOutputStream());

        boolean isHave = true;
        if (phone != null && Pattern.matches("^(1[3-8])\\d{9}$", phone)) {
            if (setPhone.contains(phone)) {
                isHave = false;
            }
            if (isHave) {
                setPhone.add(phone);
            }
        }
    }


    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    public void init() throws ServletException {
        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream("phone.set"));
            setPhone = (HashSet<String>) in.readObject();   //读取数据
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void destroy() {
        try {
            ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("phone.set"));
            o.writeObject(setPhone);   //写入数据
            System.out.println("destory()方法被调用");
            o.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
